#ifndef DOTMODEL_H
#define DOTMODEL_H

#include <QObject>
#include <QHash>

#include "../DataStructure/OneD2TwoD.h"
#include "../DrawingElements/Dot.h"
#include "../DrawingElements/Line.h"
#include "../DrawingElements/Cell.h"
#include "../DataStructure/FunctorLine.h"

class DotModel : public QObject
{
Q_OBJECT
public:
    DotModel(bool bInitField = false,QObject *parent = 0);
    ~DotModel();

signals:
    void needToResized(int nWidth,int nHeight);

public slots:

public:
    void setDotsHorz_Vert(unsigned int nDotsHorz,unsigned int nDotsVert,bool bInitField = false);
    unsigned int dotsHorizontal();
    unsigned int dotsVertical();
    void setLineLengthHorz_Vert(unsigned int nLineLenHorz,unsigned int nLineLenVert,bool bInitField = false);
    unsigned int lineLengthHorz();
    unsigned int lineLengthVert();
    void setStartPoint(QPoint ptStart,bool bInitField = false);
    QPoint startPoint();
    QRect fieldRect();
    QList<Dot*> dotsNearToPoint(QPoint pos);
    QList<Dot*> dotsEligibleFrom(Dot *dot);
    int addLine(Line *pLine);
    int removeLine(Line *pLine);
    OneD2TwoD<Cell*> *cells();
    QList<Line*> *lines();
    OneD2TwoD<Dot*> *dots();
    unsigned int playerCellsCount(Game::Player player);
    uint totalCells();
    uint totalLines();
    uint totalDots();
    QList<Cell*> cellsWinableNow();
    QList<Cell*> cellsWinableLater();
    void reset();
    QList<QLine> getSidesThatAreNotAddedDependingOnAdjacentCells(int nAdjacentCellLineCount,Cell *pCell);
    QLine getWinLineAndNextCell(Cell *pCell,Cell **pNextCell);
    bool isLineAdded(QLine line);

private:
    void initField();
    void caclWinner();
    void clearField();
    QLine checkLeftCell(Cell *pCell,int nAdjacentCellLineAddedCount, Cell **pNextCell);
    QLine checkTopCell(Cell *pCell,int nAdjacentCellLineAddedCount, Cell **pNextCell);
    QLine checkRightCell(Cell *pCell,int nAdjacentCellLineAddedCount, Cell **pNextCell);
    QLine checkBottomCell(Cell *pCell,int nAdjacentCellLineAddedCount, Cell **pNextCell);

private:    
    unsigned int mnDotsHorzontal,mnDotsVertical, mnRows,mnColumns;
    unsigned int mnLineLenHorizontal,mnLineLenVertical;
    QPoint mptStart;
    OneD2TwoD<Cell*> mCells;
    OneD2TwoD<Dot*> mDots;
    QList<Line*> mLines;
    unsigned int mnPlayer1Cells,mnPlayer2Cells;
    QRect mRectField;
    uint mnTotalLines;
};

#include "../DataStructure/FunctorLine.cpp"
#include "../DataStructure/OneD2TwoD.cpp"

#endif // DOTMODEL_H
